/**
 * Created by Delicate on 2017/5/31.
 */
$(document).ready(function(){
    init();
    monitor();
    service();
});
    
function init(){
    load(1);
    $("#jumpVal").keydown(function(e) {  
	        if (e.keyCode == 13) {  
	              jump();
	        }  
	   }); 
   }
function monitor(){
    $(".u-select-option").on("click", function () {
        if (!$(this).hasClass("u-select-option-active")) {
            $(".u-select-option-active").removeClass("u-select-option-active");
            $(this).addClass("u-select-option-active");
            load(1);
        }
    });
    $(".task-search-btn").on("click",function(res){
        load(1);
    })
    
}
function service(){

}

function load(number){
    $.ajax({
        type:"post",
        url:"/platform/task/list",
        datatype:"json",
        data:{
            pageNum: number,
            pageSize: 4,
            taskName: $("#taskName").val().trim(),
            status: $(".u-select-option-active").data("status")
        },
        success:function(data){
            let _html = "";
            if(data.list.length>0){
                for (var i=0;i<data.list.length;i++){
                    const item = data.list[i];
                    _html += '<tr> ' +
                        '<td>'+item.task_start_time.split(" ")[0]+'</td> ' +
                        '<td>'+item.task_title+'</td> ' +
                        '<td style="color: #f97a5b;">'+item.click+'</td> ' +
                        '<td style="color: #f97a5b;">'+item.cvs+'</td> ';
	                    if(item.isPayed){
	                    	_html+='<td>'+item.status+'(已支付)</td> ';
	                    }else{
	                    	_html+='<td>'+item.status+'(未支付)</td> ';
	                    }
	                    _html+='<td style="text-align:right;padding-right:25px;"> ';
                        if (item.h5_type==2 && item.isPayed){
                            _html+='<div class="table-link" style="border-radius:3px;"  onclick="openLink('+item.id+')">排序</div> ';
                        }if(!item.hasApply || !item.isPayed){
                        	_html+='<div class="table-detail" style="border-radius:3px;"  onclick="editTask('+item.id+')">编辑任务</div> ';
                        }
                        _html+='<div class="table-detail" style="border-radius:3px;"  onclick="openDetail('+item.id+')">详情</div> ' +
                        '</td> ' +
                        '</tr>';
                }
                page(data.totalPage,data.pageNumber);
                if(data.totalPage>1){
                	$("#m_choice_page_container").show();
                }else{
                	$("#m_choice_page_container").hide();
                }
            }else{
            	_html="<tr><td colspan='6' style='text-align:center;font-weight:500;padding-top:27px;position:initial;'>当前没有检索到任何相关信息</td>"
            	$("#m_choice_page_container").hide();
            }
            $("#tbody").html(_html);
        }
    })
}
var totalPage=1;
function page(total, index){
	totalPage =total;
    laypage({
        cont: 'm_choice_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
        pages: total||1, //通过后台拿到的总页数
        curr: index || 1, //当前页
        jump: function (obj, first) { //触发分页后的回调
            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                load(obj.curr);
            }
        }
    });
}

function jump(){
	var jumpVal=$("#jumpVal").val();
	var re = /^[0-9]+.?[0-9]*$/;
	if (!re.test(jumpVal)) {
		layer.alert("请输入正确的整数");
        return;
	}else if(jumpVal<=0){
		layer.alert("请输入大于1的整数");
        return;
	}else if(jumpVal>totalPage){
		layer.alert("已经超出最大页码数了");
        return;
	}else if(jumpVal&&jumpVal<=totalPage){
		load(jumpVal);
	}
}

function openLink(id){
    location.href = "/platform/task/taskSource?id="+id;
}

function editTask(id){
    location.href = "/platform/task/editTask?id="+id;
}

function openDetail(id){
    location.href = "/platform/task/detail?id="+id;
}


